# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ///////////////////////////////////////////////////////////////
# @File    :   displayCheckReportUI.py
# @Time    :   2024/08/30 18:25:50
# @Author  :   kai.luo
# @Version :   1.0.0
# @Desc    :   None
# ///////////////////////////////////////////////////////////////

# type: ignore
import traceback
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *


class DisplayCheckReport(object):
    """
    展示实验报告单的UI界面
    """

    def __init__(self, mainUI) -> None:
        self.mainUI = mainUI

    def displayCheckReport(self, img_path, all_image_formData_dict):
        # 获取当前点击的图片路径对应的数据
        image_data = all_image_formData_dict.get(img_path, '')
        if image_data:
            self.mainUI.pushButton_3.setStyleSheet("color: black;")
            self.mainUI.pushButton_3.setText('识别结果')

            try:
                # 填写识别结果标题
                if image_data[0]:
                    header_title = image_data[0]
                    hospital_name = header_title.get('检测医院名称', '')
                    self.mainUI.pushButton_3.setText(f'识别结果-{hospital_name}')

                # 填写识别结果基本信息
                if image_data[1]:
                    self.formBaseInfo(image_data[1])

                # 填写识别结果项目
                if image_data[2]:
                    self.formProductInfo(image_data[2])

                # 填写识别结果其他信息
                if image_data[3]:
                    self.formOtherInfo(image_data[3])
            except:
                print(traceback.print_exc())
        else:
            self.mainUI.pushButton_3.setStyleSheet("color: red;")
            self.mainUI.pushButton_3.setText(f'识别结果-未找到数据，请稍后再试试！')

    def formBaseInfo(self, header_data):
        name = header_data.get('姓名', '')
        self.mainUI.nameEdit.setText(name)
        sex = header_data.get('性别', '')
        self.mainUI.sexEdit.setText(sex)
        age = header_data.get('年龄', '')
        self.mainUI.ageEdit.setText(age)
        medicalRecordNumber = header_data.get('病历号', '')
        self.mainUI.numberEdit.setText(medicalRecordNumber)
        department = header_data.get('科室', '')
        self.mainUI.departmentEdit.setText(department)
        specimenCondition = header_data.get('标本情况', '')
        self.mainUI.situationEdit.setText(specimenCondition)
        phoneNumber = header_data.get('联系电话', '')
        self.mainUI.telEdit.setText(phoneNumber)
        samplingTime = header_data.get('采样时间', '')
        self.mainUI.samplingEdit.setText(samplingTime)
        receivingTime = header_data.get('接收时间', '')
        self.mainUI.recevieEdit.setText(receivingTime)

    def formProductInfo(self, data):
        # 设置表格行数和列数
        rows = len(data)
        cols = len(data[0])
        self.mainUI.tableWidget.setRowCount(rows)
        self.mainUI.tableWidget.setColumnCount(cols)
        # 表头
        self.mainUI.tableWidget.setHorizontalHeaderLabels(['项目', '结果', '单位', '参考', '提示'])
        # 填充表格数据
        for row in range(0, rows):
            for col in range(cols):
                item = QTableWidgetItem(data[row][col])
                if col == 4 and data[row][col] in ['偏低', '偏高']:
                    item.setForeground(QBrush(QColor('red')))
                self.mainUI.tableWidget.setItem(row, col, item)

        # 自适应列宽和行高
        self.mainUI.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)  # type: ignore

    def formOtherInfo(self, footer_data):
        inspector = footer_data.get('检验人', '')
        self.mainUI.inspectorEdit.setText(inspector)
        reviewer = footer_data.get('审核人', '')
        self.mainUI.reviewerEdit.setText(reviewer)
        hospitalPphoneNumber = footer_data.get('医院电话', '')
        self.mainUI.hospitalTelEdit.setText(hospitalPphoneNumber)
        reportDate = footer_data.get('报告日期', '')
        self.mainUI.reportDataEdit.setText(reportDate)
        hospitalAddress = footer_data.get('医院地址', '')
        self.mainUI.hospitalAddrEdit.setText(hospitalAddress)
